package com.kk.xhr.admin.biz.impl;

import com.kk.xhr.admin.biz.ILevelBizService;
import com.kk.xhr.admin.controller.vo.LevelVO;
import com.kk.xhr.common.ErrorCodeEnum;
import com.kk.xhr.common.exception.BusinessException;
import com.kk.xhr.common.pojo.BinaryEnum;
import com.kk.xhr.model.dto.LevelDTO;
import com.kk.xhr.model.service.ILevelService;
import com.kk.xhr.model.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 等级biz服务接口实现
 *
 * @author kkmystery
 * @version 1.0 2021/3/5
 * @since 1.0.0
 */
@Repository
@RequiredArgsConstructor
public class LevelBizServiceImpl implements ILevelBizService {
    private final ILevelService levelService;
    private final IUserService userService;

    /**
     * {@inheritDoc}
     */
    @Override
    public List<LevelVO> query() {
        List<LevelDTO> list = levelService.query();
        if (0 == list.size()) {
            return Collections.emptyList();
        }
        return list.stream().map(dto -> LevelVO.builder()
            .id(dto.getId())
            .level(dto.getLevel())
            .status(dto.getStatus())
            .build())
            .collect(Collectors.toList());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean add(String level) {
        return levelService.add(LevelDTO.builder()
            .level(level)
            .build());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean modify(Long id, String level) {
        return levelService.modify(LevelDTO.builder()
            .id(id)
            .level(level)
            .build());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean modifyStatus(Long id, BinaryEnum status) {
        if (BinaryEnum.FALSE == status) {
            if (userService.existLevel(id)) {
                throw new BusinessException(ErrorCodeEnum.LEVEL_DELETE_FAIL);
            }
        }
        return levelService.modifyStatus(id, status);
    }
}
